<template>
    <div id="addEmail">
        <yd-cell-group style="margin-bottom:0">
            <yd-cell-item>
                <span slot="left">邮箱：</span>
                <yd-input slot="right" required :show-required-icon='false' regex="email" v-model="emailName" ref="emailName" placeholder="请输入邮箱"></yd-input>
            </yd-cell-item>
        </yd-cell-group>
        <div class="dangerBtn">
            <yd-button size="large" @click.native="dangerBtn" type="danger">保存邮箱</yd-button>
        </div>
    </div>
</template>
<script>

export default {
    name: 'addEmail',
    data() {
        return {
            emailName: '',
        }
    },
    created: function() {
        this.init();
    },
    methods: {
        init() {
            this.$dialog.loading.open('请稍后');
            this.$axios.get(this.CONSTANT.wapiBaseUrl + 'member/memberDetail')
                .then((response) => {
                    this.$dialog.loading.close();
                    if (response.status == 200) {
                        this.emailName = response.data.object.memberEmail;
                    }
                }).catch((error) => {
                    this.$dialog.loading.close();
                });
        },
        dangerBtn() {
            let emailName = this.$refs.emailName;
            if (this.emailName == '') {
                this.$dialog.toast({
                    mes: '邮箱不可为空',
                    timeout: 600
                });
                return false;
            }
            if (!emailName.valid) {
                this.$dialog.toast({
                    mes: '请输入正确的邮箱',
                    timeout: 600
                });
                return false;
            }
            this.$dialog.loading.open('请稍后');
            this.$axios.post(this.CONSTANT.wapiBaseUrl + 'member/bindMemberEmail?email=' + this.emailName)
                .then((response) => {
                    this.$dialog.loading.close();
                    if (response.status == 200) {
                        this.$dialog.toast({
                            mes: '保存成功',
                            timeout: 600
                        });
                        this.$router.go(-1);
                    }
                }).catch((error) => {
                    this.$dialog.loading.close();
                });
        }
    }
}
</script>
<style  scoped>
.dangerBtn {
    width: 100%;
    box-sizing: border-box;
    padding: 0 0.25rem;
    height: auto;
    overflow: hidden;
}
</style>